

import UIKit

class ViewController: UIViewController {

    
    @IBOutlet weak var userName: UITextField!
    
    @IBOutlet weak var password: UITextField!
    
    @IBOutlet weak var btnLogin: UIButton!
    
    //正则表达式匹配正确的密码，没有特殊字符
    func checkPassword(str:String) -> Bool {
        do{
            let pattern = "[A-Za-z0-9]+";
            let regex:NSRegularExpression = try NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.caseInsensitive);
            let matches = regex.matches(in: str, options: NSRegularExpression.MatchingOptions.reportProgress, range: NSMakeRange(0, str.characters.count))
            return matches.count > 0;
        }
        catch{
            return false;
        }
    }
    //检查密码有没有相同字符
    func checkPasswordHasNoSameCharacter(str:String) -> Bool {
        //swift3 已经没有for(var i = 0 ; i < str.length;++i)这样的语法了
        var i = 0;
        for temp in str.characters{
            i += 1;
            let index = str.index(str.startIndex,offsetBy:i);
            let s = str.substring(from:index);
            if(s.contains(String(temp))){
                return false
            }
        }
        return true;
    }
    //检测字符串中不包含shift和dead
    func checkPasswordHasNoString(str:String,word_1:String,word_2:String) -> Bool {
        if((str.contains(word_1))||(str.contains(word_2))){
            return false;
        }
        return true;
    }
    @IBAction func unableToLogin(_ sender: UIButton) {
        
        //弹框对话控制器
        let alertController = UIAlertController(title: "对不起", message: "本功能未实现", preferredStyle: UIAlertControllerStyle.alert)
        //取消动作
        let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel, handler: {(UIAlertAction)->Void in
            print("取消操作");
        });
        //确认动作
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil);
        //添加动作
        alertController.addAction(cancelAction);
        alertController.addAction(okAction);
        //在self上弹出一个视图
        self.present(alertController, animated: true, completion:nil)
    }
    
    @IBAction func newComer(_ sender: UIButton) {
        let alertController = UIAlertController(title: "对不起", message:"本功能未实现", preferredStyle: UIAlertControllerStyle.alert);
        let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel, handler: {(UIAlertAction)->Void in
            print("取消操作");
        });
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil);
        alertController.addAction(cancelAction);
        alertController.addAction(okAction);
        self.present(alertController, animated: true, completion:nil);

    }
    
    @IBAction func btnLogin(_ sender: UIButton) {
        
        let userNameString = userName.text?.trimmingCharacters(in: NSCharacterSet.whitespaces);
        let passwordString = password.text?.trimmingCharacters(in: NSCharacterSet.whitespaces);
        var flag = true;
        //用户名密码不为空
        if((userNameString != "")&&(passwordString != ""))
        {
            if((passwordString?.characters.count)! > 8){
                flag = checkPassword(str: passwordString!);
                flag = checkPasswordHasNoSameCharacter(str: passwordString!);
                flag = checkPasswordHasNoString(str: passwordString!, word_1: "shift",word_2: "dead");
            }
            else{
                flag = false;
            }
        }
        else
        {
            flag = false;
        }
        if(flag)
        {
            //bundle表示路径，completion表示回调函数
            let sb = UIStoryboard(name: "Main", bundle: nil);
            let secondViewController = sb.instantiateViewController(withIdentifier: "second") as! SecondPageController;
            self.present(secondViewController, animated: true, completion: nil);
        }
        else
        {
            let alertController = UIAlertController(title: "对不起", message:"您的输入有误", preferredStyle: UIAlertControllerStyle.alert);
            let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil);
            alertController.addAction(okAction);
            self.present(alertController, animated: true, completion:nil)
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        userName.adjustsFontSizeToFitWidth=true  //当文字超出文本框宽度时，自动调整文字大小
        userName.minimumFontSize=14  //最小可缩小的字号
        
        //设置view的背景图片
        self.view.backgroundColor = UIColor(patternImage: UIImage(named:"login.jpg")!)
        
        //圆角按钮
        btnLogin.layer.cornerRadius = 4.0;
        btnLogin.layer.masksToBounds = true;
        //圆形头像
        let headImageView = UIImageView();
        let headImage = UIImage(named:"wyh.jpg");
        let headImageSize:CGFloat = 75.0;
        
        //frame设置其位置和大小，而bounds只能设置其大小，其参数中的x、y不起作用即便是之前没有设定frame属性，控件最终的位置也不是bounds所设定的参数。bounds实现的是将UIImageView控件以原来的中心为中心进行缩放。
        headImageView.bounds = CGRect(x:(self.view.bounds.size.width - headImageSize)/2,y:(self.view.bounds.size.width - headImageSize)/2-150,width:headImageSize,height:headImageSize);
        
        headImageView.frame = CGRect(x:(self.view.bounds.size.width - headImageSize)/2,y:(self.view.bounds.size.width - headImageSize)/2-75,width:headImageSize,height:headImageSize)
        
        //设置圆形的半径
        headImageView.layer.cornerRadius = headImageView.bounds.height/2;
        //设置遮罩，true代表显示在最上面，iOS中视图可以理解为一层一层的图层
        headImageView.layer.masksToBounds = true;
        //圆形边框的属性
        headImageView.layer.borderColor = UIColor.gray.cgColor;
        headImageView.layer.borderWidth = 3;
        
        headImageView.image = headImage;
        self.view.addSubview(headImageView);
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

